using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Andrewl.XnaHelpers.GameObjects
{
    public class Bar
    {
        public BarType type;

        public Rectangle background;

        public Rectangle foreground;

        public Color backColor;

        public Color frontColor;

        public int maxWidth;

        public Texture2D frontTexture;

        public Texture2D backTexture;

        public bool frontTextured;

        public bool backTextured;

        public Bar(BarType BarType, Point Position, int Width, int Height, Color BackgroundColor, Color ForegroundColor)
        {
            type = BarType;
            background = new Rectangle(Position.X, Position.Y, Width, Height);
            foreground = new Rectangle(Position.X, Position.Y, Width, Height);
            backColor = BackgroundColor;
            frontColor = ForegroundColor;
            maxWidth = Width;
            frontTextured = false;
            backTextured = false;
        }

        public Bar(Texture2D frontTextureArg, Color backColorArg, BarType BarTypeArg, Point Position, int Width, int Height)
        {
            frontTexture = frontTextureArg;
            backColor = backColorArg;
            type = BarTypeArg;
            background = new Rectangle(Position.X, Position.Y, Width, Height);
            foreground = new Rectangle(Position.X, Position.Y, Width, Height);
            frontTextured = true;
            backTextured = false;
            maxWidth = Width;
        }

        public Bar(Color frontColorArg, Texture2D backTextureArg, BarType BarTypeArg, Point Position, int Width, int Height)
        {
            frontColor = frontColorArg;
            backTexture = backTextureArg;
            type = BarTypeArg;
            background = new Rectangle(Position.X, Position.Y, Width, Height);
            foreground = new Rectangle(Position.X, Position.Y, Width, Height);
            frontTextured = false;
            backTextured = true;
            maxWidth = Width;
        }

        public Bar(Texture2D frontTextureArg, Texture2D backTextureArg, BarType BarTypeArg, Point Position, int Width, int Height)
        {
            frontTexture = frontTextureArg;
            backTexture = backTextureArg;
            type = BarTypeArg;
            background = new Rectangle(Position.X, Position.Y, Width, Height);
            foreground = new Rectangle(Position.X, Position.Y, Width, Height);
            frontTextured = true;
            backTextured = true;
            maxWidth = Width;
        }

        public void Subtract(int amount)
        {
            switch (type)
            {
                case BarType.SubtractRight:
                    foreground.Width -= amount;
                    if (foreground.Width < 0)
                        foreground.X = background.X;
                    break;

                case BarType.SubtractLeft:
                    foreground.Width -= amount;
                    foreground.X += amount;
                    if (foreground.Width < 0)
                    {
                        foreground.Width = 0;
                        foreground.X = background.Right;
                    }
                    break;

                case BarType.SubtractBoth:
                    foreground.Width -= amount * 2;
                    foreground.X += amount;
                    if (foreground.Width < 0)
                    {
                        foreground.Width = 0;
                        foreground.X = background.Center.X;
                    }
                    break;
            }


        }

        public void Add(int amount)
        {
            switch (type)
            {
                case BarType.SubtractRight:
                    foreground.Width += amount;
                    if (foreground.Width > maxWidth)
                        foreground.Width = maxWidth;
                    break;
                case BarType.SubtractLeft:
                    foreground.Width += amount;
                    foreground.X -= amount;
                    if (foreground.Width > maxWidth)
                    {
                        foreground.Width = maxWidth;
                        foreground.X = background.X;
                    }
                    break;
                case BarType.SubtractBoth:
                    foreground.Width += amount * 2;
                    foreground.X -= amount;
                    if (foreground.Width > maxWidth)
                    {
                        foreground.Width = maxWidth;
                        foreground.X = background.X;
                    }
                    break;
            }
        }

        public void Draw()
        {
            if (!backTextured)
            {
                XnaHelpers.SpriteBatch.Draw(XnaHelpers.WhitePixel, background, backColor);
            }
            else
            {
                XnaHelpers.SpriteBatch.Draw(backTexture, background, null, Color.White);
            }
            if (!frontTextured)
            {
                XnaHelpers.SpriteBatch.Draw(XnaHelpers.WhitePixel, foreground, frontColor);
            }
            else
            {
                XnaHelpers.SpriteBatch.Draw(frontTexture, foreground, Color.White);
            }
        
        }

    }
}